Ti.include('../class/db.js');
Ti.include('../class/network.js');
Ti.include('../viewHelper/ideaDetailsViews.js');

var win = Ti.UI.currentWindow;

var objIdea = Db.retrieveIdea(win.idIdea);
var objSolucions = Db.retrieveSolucionsByIdeaId(win.idIdea);
var views = ideaDetailsViews.getViews(objIdea);

function findLinks (tx){
	var response = [];
	var exp =/(\b(https?|ftp|file):\/\/[\-A-Z0-9+&@#\/%?=~_|!:,.;]*)/mig;
	var hits = tx.match(exp);
	if (hits) {
		for (var i in hits){
			if (!isNaN(i)){
				response.push(hits[i]);
			}
		}
		Ti.API.info("links trobats:"+response);
	}else{
		Ti.API.info("link no trobat text");
	}
	return response;
}
var links = findLinks(objIdea.ds);

if (links.length >0){
	links.push('Cancel');
	var save = Ti.UI.createButton({
		title:L('links')
	});
	save.addEventListener('click', function()
	{
		var dialog = Ti.UI.createOptionDialog({
			options:links,
			cancel:links.length -1,
			title:L('Enllaços en el text')
		});
		dialog.addEventListener('click',function(e){ Ti.Platform.openURL(links[e.index]);});
		dialog.show();
	});
	win.barColor = '#333';
	win.toolbar =[save];
}

win.add(views.activityIndicator);
win.addEventListener('focus', function (e){
	Ti.API.info("ideaDetailsWindow focus");
	Db.markIdeaAsRead(win.idIdea);
	Ti.App.fireEvent('fillTorrentTablesFromDatabase');
	Ti.App.tabGroup.animate({bottom:-50,duration:500});
});

win.addEventListener('close', function (e){
	Ti.API.info("ideaDetailsWindow close");
	Ti.App.tabGroup.animate({bottom:0,duration:500});
	
});

function getFormatedDate(newDate,pubDate){
	newDate.setTime( pubDate * 1000 );
	var sDay = newDate.getDate() +"";
	if (sDay.length==1){
		sDay= '0'+sDay;
	}
	var sMonth = (newDate.getMonth() + 1) +"";
	if (sMonth.length==1){
		sMonth= '0'+sMonth;
	}
	var sYear = newDate.getUTCFullYear();
	var sHours = newDate.getHours() + "";
	if (sHours.length==1){
		sHours= '0'+sHours;
	}
	var sMinutes = newDate.getMinutes() + "";
	if (sMinutes.length==1){
		sMinutes= '0'+sMinutes;
	}
	var sSeconds = newDate.getSeconds() + "";
	if (sSeconds.length==1){
		sSeconds= '0'+sSeconds;
	}
	
	var sDate = sDay+ '/'+ sMonth+ '/' + sYear + ' ' + sHours + ":" + sMinutes + ":" + sSeconds;
	return sDate;
	
}

Ti.App.addEventListener('comentarisDownloadOk', function(response){
	views.activityIndicator.hide();
	//Ti.API.info("comentarisDownloadOk response:"+response.text);
	var responseJson = JSON.parse(response.text);
	var rowsComentaris = [];
	var currentComentari = null;
	var tmpRow = null;
	var newDate = new Date();
	var dateString = null;
	
	for(var i in responseJson){
		if (!isNaN(i)){
			currentComentari = responseJson[i];
			dateString =  getFormatedDate(newDate,currentComentari.pubDate);
			currentComentari.headerText = currentComentari.author + " " + L("at") + " " + dateString;
			rowsComentaris = ideaDetailsViews.getComentariRowFromData(rowsComentaris,currentComentari);
		}
	}
	if (rowsComentaris.length == 0){
		tmpRow = ideaDetailsViews.getNoComentariRow();
		rowsComentaris.push(tmpRow);
	}
	views.comentarisTableView.setData(rowsComentaris);
});

Ti.App.addEventListener('comentarisDownloadKo', function(status){
	views.activityIndicator.hide();
	alert("ERROR "+ status);
});

Ti.App.addEventListener('startDownloadingComments', function (){
	views.activityIndicator.show();
	//Ti.API.info("views.activityIndicator.message:"+views.activityIndicator.message);
	network.getComentaris(win.idIdea);
});

win.titleControl.addEventListener('click', function(e){
	Ti.API.info("barraBotons.click: e.index:"+e.index);
	switch (e.index){
		case 0: // ideas
			views.ideasTableView.show();
			views.solucionsTableView.hide();
			views.comentarisTableView.hide();
			break;
		
		case 1: // solucions
			views.ideasTableView.hide();
			views.solucionsTableView.show();
			views.comentarisTableView.hide();
			break;
		
		case 2: // comentaris
			views.ideasTableView.hide();
			views.solucionsTableView.hide();
			views.comentarisTableView.show();
			Ti.App.fireEvent('startDownloadingComments');
			break;
	}
});

views.solucionsTableView.addEventListener('click',  function(elem){
	var idSolucio = elem.rowData.idSolucio;
	var numSolucio = elem.rowData.numSolucio;
	if (idSolucio != null){
		var solucioWindow = ideaDetailsViews.getSolucioWindowByData(idSolucio,numSolucio); 
		Ti.App.ideaNavigationGroup.open(solucioWindow,{animated:true});
	}
});

views.titolIdeaView.add(views.titolIdeaLabel);
win.add(views.titolIdeaView);

views.ideasTableView.top = views.titolIdeaView.height;
views.solucionsTableView.top = views.titolIdeaView.height;
views.comentarisTableView.top = views.titolIdeaView.height;

views.ideaDetailsRow.add(views.ideaDescriptionLabel);
views.ideasTableView.setData([views.ideaDetailsRow]);


win.add(views.ideasTableView);
win.add(views.solucionsTableView);
win.add(views.comentarisTableView);

views.solucionsTableView.setData(ideaDetailsViews.getSolucionsRowsFromData(objSolucions));